//
//  SleepVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "SleepVC.h"
#import "SleepListView.h"
#import <JYCPanView/JYCPanView.h>
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "SleepHeadModel.h"
#import "WMPlayer.h"

@interface SleepVC ()<SDCycleScrollViewDelegate>
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UILabel * infoLabel;
@property (nonatomic, strong) JYCPanView *panView;
@property (nonatomic,strong) SleepListView * listView;
@property (nonatomic,strong) SDCycleScrollView * bannerView;
@property(nonatomic,strong)  WMPlayer * wmPlayer;
@property(nonatomic,strong)  WMPlayer * musicPlayer;
@property (nonatomic,strong) UIView * backView;
@property (nonatomic,strong) UIButton * playBtn;
@property (nonatomic,strong) NSMutableArray * listArray;

@end

@implementation SleepVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewLayout];
    [self requestMusicList];
}

-(void)setSubViewLayout{
    
    self.bgView.image = [UIImage imageNamed:@"Sleep_bg_icon"];
    self.navgationView.iconView.image = [UIImage imageNamed:@"Home_titlte_icon_write"];
    self.navgationView.backgroundColor = UIColor.clearColor;
    
    [self.view addSubview:self.musicPlayer];
    [self.view addSubview:self.wmPlayer];
    [self.view addSubview:self.bannerView];
    [self.view addSubview:self.backView];
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.infoLabel];
    [self.view addSubview:self.playBtn];
    [self.view addSubview:self.panView];
    [self.panView addSubview:self.listView];
    
    [self.view bringSubviewToFront:self.navgationView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.wmPlayer pause];
    [self.musicPlayer pause];
    self.playBtn.selected = NO;
}


-(void)requestData{
    @WeakObj(self)
    [SPNetworkingIns GET:PORT(@"sleep-center/api/sleep/SleepMusicHead") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        SleepHeadModel * model = [SleepHeadModel yy_modelWithDictionary:responseObject[@"data"]];
        self.nameLabel.text = model.timeZone;
        self.infoLabel.text = model.health;
        [self.listView setInfoModel:model];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(void)requestMusicList{
    [self.listArray removeAllObjects];
    [SPNetworkingIns GET:PORT(@"sleep-center/api/sleep/SleepMusicMp4") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        NSArray * dataArr = [responseObject objectForKey:@"data"];
        NSMutableArray * array = [[NSMutableArray alloc]init];
        if (dataArr.count) {
            for (NSDictionary * item in dataArr) {
                SleepHeadModel * model = [[SleepHeadModel alloc]init];
                model = [SleepHeadModel yy_modelWithDictionary:item];
                [self.listArray addObject:model];
                [array addObject:@"Common_slepp_mpbg"];
            }
        }
        self.bannerView.imageURLStringsGroup = array;
        
        SleepHeadModel * model = self.listArray.firstObject;
        WMPlayerModel *playerModel = [WMPlayerModel new];
        playerModel.videoURL = [NSURL URLWithString:model.videoUrl];
        [self.wmPlayer setPlayerModel:playerModel];
        [self.wmPlayer play];
        
        WMPlayerModel *musicModel = [WMPlayerModel new];
        musicModel.videoURL = [NSURL URLWithString:model.audioUrl];
        [self.musicPlayer setPlayerModel:musicModel];
        [self.musicPlayer play];
        
        self.playBtn.selected = YES;
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(void)playBtnAction:(UIButton * )sender{
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.wmPlayer play];
        [self.musicPlayer play];
    }else{
        [self.wmPlayer pause];
        [self.musicPlayer pause];
    }
}

-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
    [self.wmPlayer resetWMPlayer];
    [self.musicPlayer resetWMPlayer];
    
    SleepHeadModel * model = self.listArray[index];
    WMPlayerModel *playerModel = [WMPlayerModel new];
    playerModel.videoURL = [NSURL URLWithString:model.videoUrl];
    [self.wmPlayer setPlayerModel:playerModel];
    [self.wmPlayer play];
    
    WMPlayerModel *musicModel = [WMPlayerModel new];
    musicModel.videoURL = [NSURL URLWithString:model.audioUrl];
    [self.musicPlayer setPlayerModel:musicModel];
    [self.musicPlayer play];
    self.playBtn.selected = YES;
    
}

-(UIView *)backView{
    
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _backView.backgroundColor = RDRGBA(0x6B5B47, 1);
        _backView.hidden = YES;
    }
    return _backView;
}

-(UILabel *)nameLabel{
    
    if(!_nameLabel){
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * FMScaleX, NavHeight + 10 * FMScaleX, 150 * FMScaleX, 30 * FMScaleX)];
        _nameLabel.font = Bold(30);
        _nameLabel.textColor = UIColor.whiteColor;
    }
    return _nameLabel;
}
-(UILabel *)infoLabel{
    
    if(!_infoLabel){
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * FMScaleX, GETMaxY(self.nameLabel) + 10 * FMScaleX, WIDTH - 40 * FMScaleX, 70 * FMScaleX)];
        _infoLabel.textColor = UIColor.whiteColor;
        _infoLabel.font = Regular(13);
        _infoLabel.numberOfLines = 0;
    }
    return _infoLabel;
}

- (JYCPanView *)panView
{
    if (!_panView) {
        _panView = [[JYCPanView alloc] initWithFrame:CGRectMake(0, HEIGHT - TabBarHeight - kSafeAreaHeight - 150 * FMScaleX, WIDTH, HEIGHT - TabBarHeight - kSafeAreaHeight - NavHeight - 250 * FMScaleX)];
        _panView.panDerection = JYCPanDerectionVertical;
        _panView.minX = 0;
        _panView.maxX = 0;
        _panView.midX = 0;
        _panView.minY = GETMaxY(self.infoLabel) + 10 * FMScaleX;
        _panView.midY = GETMaxY(self.infoLabel) + 10 * FMScaleX;
        _panView.maxY = HEIGHT - TabBarHeight - kSafeAreaHeight - 150 * FMScaleX;

        @WeakObj(self)
        _panView.viewHasSlideVerticalMax = ^(BOOL isMax) {
            @StrongObj(self)
            if (isMax) {
                self.backView.hidden = YES;
                self.playBtn.hidden = NO;
            }else{
                self.backView.hidden = NO;
                self.playBtn.hidden = YES;
            }
        };
    }
    return _panView;
}

-(WMPlayer *)wmPlayer{
    
    if (!_wmPlayer) {
        _wmPlayer = [[WMPlayer alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT  - kSafeAreaHeight  - TabBarHeight - 20 * FMScaleX) ];
        _wmPlayer.backBtnStyle = BackBtnStyleNone;
        _wmPlayer.rate = 1.0;
        _wmPlayer.enableVolumeGesture = NO;
        _wmPlayer.enableFastForwardGesture = NO;
        _wmPlayer.playerLayerGravity = WMPlayerLayerGravityResizeAspectFill;
        _wmPlayer.loopPlay = YES;
        [_wmPlayer hideBarView];
    }
    return _wmPlayer;
}

-(WMPlayer *)musicPlayer{
    
    if (!_musicPlayer) {
        _musicPlayer = [[WMPlayer alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT  - kSafeAreaHeight  - TabBarHeight - 20 * FMScaleX) ];
        _musicPlayer.backBtnStyle = BackBtnStyleNone;
        _musicPlayer.rate = 1.0;
        _musicPlayer.enableVolumeGesture = NO;
        _musicPlayer.enableFastForwardGesture = NO;
        _musicPlayer.playerLayerGravity = WMPlayerLayerGravityResizeAspectFill;
        _musicPlayer.loopPlay = YES;
        [_musicPlayer hideBarView];
    }
    return _musicPlayer;
    
}

-(SDCycleScrollView *)bannerView{
    
    if (!_bannerView) {
        _bannerView = [[SDCycleScrollView alloc]initWithFrame:CGRectMake(0, GETMaxY(self.infoLabel) + 10 * FMScaleX, WIDTH, 400 * FMScaleX)];
        _bannerView.backgroundColor = UIColor.clearColor;
        _bannerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _bannerView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _bannerView.pageControlDotSize = CGSizeMake(6 * FMScaleX, 6* FMScaleX);
        _bannerView.currentPageDotColor = RDRGBA(0xffffff, 0.7);
        _bannerView.pageDotColor = RDRGBA(0xffffff, 0.3);
        _bannerView.placeholderImage = [UIImage imageNamed:@"Common_slepp_mpbg"];
        _bannerView.autoScroll = NO;
        _bannerView.infiniteLoop = NO;
        _bannerView.delegate = self;
    }
    return _bannerView;
}

-(SleepListView *)listView{
    
    if (!_listView) {
        _listView = [[SleepListView alloc]initWithFrame:self.panView.bounds];
    }
    return _listView;
    
}
-(UIButton *)playBtn{
    
    if (!_playBtn) {
        _playBtn = [[UIButton alloc]initWithFrame:CGRectMake((WIDTH - 50 * FMScaleX) / 2, (HEIGHT - 50 * FMScaleX) / 2, 50 * FMScaleX, 50 * FMScaleX)];
        [_playBtn setImage:[UIImage imageNamed:@"Sleep_play_player"] forState:UIControlStateNormal];
        [_playBtn setImage:[UIImage imageNamed:@"Sleep_play_puse"] forState:UIControlStateSelected];
        [_playBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}

-(NSMutableArray *)listArray{
    
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}
@end
